package com.goods.business.service.imp;

import com.goods.business.converter.ProductCategoryConverter;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
@SuppressWarnings("all")
public class ProductCategoryServiceImpl implements ProductCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    /**
     * 查询所有物质分类
     * @return
     */
    @Override
    public List<ProductCategoryTreeNodeVO> findAllCategory() {
        //获取所有分类
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = getNodeVOS();
        //构造成树
        List<ProductCategoryTreeNodeVO> build = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);
        return  build;
    }

    /**
     * 查询所有分类并转换为树节点类型
     * @return
     */
    private List<ProductCategoryTreeNodeVO> getNodeVOS() {
        List<ProductCategory> productCategories = productCategoryMapper.selectAll();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = productCategories.stream().map(productCategory -> {
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO = ProductCategoryConverter.converterToProductCategoryTreeNodeVO(productCategory);
            return productCategoryTreeNodeVO;
        }).collect(Collectors.toList());
        return productCategoryTreeNodeVOS;
    }

    /**
     * 获取一级和二级分类
     * @return
     */
    @Override
    public List<ProductCategoryTreeNodeVO> findParentCategory() {
        List<ProductCategoryTreeNodeVO> treeNodeVOS = this.getNodeVOS();
        List<ProductCategoryTreeNodeVO> buildParent = CategoryTreeBuilder.buildParent(treeNodeVOS);
        return buildParent;
    }

    /**
     * 添加分类
     * @param productCategory
     */
    @Override
    public void addProductCategory(ProductCategory productCategory) {
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.insert(productCategory);
    }

    /**
     * 根据id查询分类信息（回显）
     * @param id
     */
    @Override
    public ProductCategory edit(Long id) {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        if (productCategory!=null){
            return productCategory;
        }
        return null;
    }

    @Override
    public void update(ProductCategory productCategory) {
        ProductCategory category = new ProductCategory();
        BeanUtils.copyProperties(productCategory,category);
        category.setModifiedTime(new Date());

        productCategoryMapper.updateByPrimaryKey(category);
    }

    @Override
    public String delete(Long id) {
        //先查询一次
        ProductCategory edit = this.edit(id);
        if (edit!=null){
            Example example = new Example(ProductCategory.class);
            Example.Criteria criteria = example.createCriteria().andEqualTo("pid", edit.getId());
            int i = productCategoryMapper.selectCountByExample(example);
            System.out.println("i = " + i);
            if (i==0){
                productCategoryMapper.deleteByPrimaryKey(edit);
                return "1";
            }
            return "0";
        }
        return "0";
    }
}
